using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Husb.Data;
using Husb.Util;
using CentralOffice.Admin.Data;
using CentralOffice.Admin.DataAccess;
using UrlTale = CentralOffice.Admin.Data.UrlDataSet.UrlDataTable;
using UrlRow = CentralOffice.Admin.Data.UrlDataSet.UrlRow;
namespace CentralOffice.WebClient.Admin.PublicAffairs.url
{
    public partial class index : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    string str_Id = Request.QueryString["id"].Trim().Trim();
                    if (string.IsNullOrEmpty(str_Id) == false)
                    {
                        UrlRow urlrow = null;
                        urlrow = CentralOffice.Admin.BusinessActions.Url.GetById(Int32.Parse(str_Id));
                        URL_TYPE.Value = urlrow.IsUrlTypeNull() ? "" : urlrow.UrlType;
                        txtNo.Value = urlrow.IsUrlNoNull()?"0":urlrow.UrlNo.ToString();
                        txtDesc.Value = urlrow.IsUrlDescNull() ? "" : urlrow.UrlDesc;
                        txtAddr.Value = urlrow.IsUrlNull() ? "" : urlrow.Url;
                        txtId.Text = str_Id;
                    }
                }
            }
        }
      
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            SaveUrl();
            txtId.Text = "";
            Response.Write("<script language=javascript>window.location.href='index.aspx';</script>");
        }

        private void SaveUrl()
        {
            string str_Id = txtId.Text.Trim();
            //String savePath = SiteUtil.GetDocumentFilePath();
            UrlDataSet url = GetFields(str_Id);
            int cnt = CentralOffice.Admin.BusinessActions.Url.Update(url);
        }

        private UrlDataSet GetFields(string id)
        {
            UrlRow urlrow = null;

            if (String.IsNullOrEmpty(id))
            {
                urlrow = (new UrlDataSet()).Url.NewUrlRow();
            }
            else
            {
                urlrow = CentralOffice.Admin.BusinessActions.Url.GetById(Int32.Parse(id));
            }
            urlrow.UrlType = URL_TYPE.Value;
            urlrow.UrlNo = Int32.Parse(txtNo.Value);
            urlrow.UrlDesc = txtDesc.Value;
            urlrow.UserId = "";
            urlrow.Url = txtAddr.Value;

            if (String.IsNullOrEmpty(id))
            {
                urlrow.Table.Rows.Add(urlrow);
            }

            return urlrow.Table.DataSet as UrlDataSet;
        }

        protected void odsUrl_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            int id = (int)e.InputParameters["UrlId"];
            e.InputParameters.Remove("UrlId");
            e.InputParameters.Add("id", id);
        }
    }
}
